home *** CD-ROM | disk | FTP | other *** search
/ CD Ware Multimedia 1995 May / cd Ware (Juegos) Epimundo.iso / DOS / PRGMMING / PWORD_MC.ZIP / GRPHTEXT.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1994-12-04  |  2.9 KB  |  130 lines

  1. // Get user input in graphics mode per keystroke
  2. // Michael J. Campbell, 1994
  3.  
  4. #include <graphics.h>
  5. #include <string.h>
  6. #include <ctype.h>
  7. #include <conio.h>
  8. #include <stdio.h>
  9. #include "keycodes.h"
  10.  
  11. extern char text[30];
  12. int ESC_pressed = 0;
  13.  
  14. void write_text(int x, int y, int tcolor, int tshadow, int blankcolor, int length, int SHADOW)
  15. /*
  16. Writes text to the screen as it is typed in the graphics mode in the default
  17. font of size 0.
  18. Call write_text(A,B,C,D,E,F,G) where:
  19.     A = starting x coordinate of the text
  20.     B = starting y coordinate of the text
  21.     C = text color
  22.     D = text shadow
  23.     E = color of surrounding that text is in, use for erasing while back
  24.         spacing
  25.     F = maximum length of the text string
  26.     G = set to 1 if you want a "stamped" look to your text
  27. */
  28.     {
  29.  
  30.     int alpha=0, beta=0, xx=0, offset;
  31.     char buffer[1];
  32.     char character[60];
  33.  
  34.  
  35.     if(SHADOW) offset = 0; //If user doesn't choose a shadowed text
  36.     else offset = 1;       //If user chooses a shadowed text
  37.  
  38.     strcpy(buffer,"");
  39.  
  40.     settextstyle(SMALL_FONT,HORIZ_DIR,4);
  41.     settextjustify(LEFT_TEXT,CENTER_TEXT);
  42.  
  43.     for(;;) //Loop until carriage return encountered
  44.         {
  45.         //Draw the cursor
  46.         setcolor(tshadow);
  47.         outtextxy(x+beta,y,"█");
  48.  
  49.         //Get a keystroke from the user
  50.         character[alpha] = getch();
  51.  
  52.         //Clear old cursor position
  53.         setcolor(blankcolor);
  54.         outtextxy(x+beta,y,"█");
  55.  
  56.         //Check for the end of the text string entered
  57.         if(character[alpha] == ENTER)
  58.             {
  59.             character[alpha] = '\0';
  60.             if(character[alpha-1] == '\\') character[alpha-1] = '\0';
  61.             break;
  62.             }
  63.         if(character[alpha] == ESC)
  64.             {
  65.             ESC_pressed = 1;
  66.             break;
  67.             }
  68.         //Check for a backspace to erase mistyped keys
  69.         if(character[alpha] == BACK_SPACE || alpha >= length)
  70.             {
  71.             //The next lines erase the old characters on the screen
  72.             //and clear the characters in the string
  73.             beta-=7;
  74.             if(beta <= 0) beta = 0;
  75.             sprintf(buffer,"%c",character[alpha-1]);
  76.             setcolor(blankcolor);
  77.             outtextxy(x+beta,y,"█");
  78.             outtextxy(x+beta+1,y+1,"█");
  79.  
  80.  
  81.  
  82.             alpha=alpha-1;
  83.  
  84.             //Don't backspace pass beginning
  85.             if(alpha < 0) alpha = 0;
  86.  
  87.             //Goto beginning of loop
  88.             continue;
  89.             }
  90.         else
  91.             {
  92.             //Put typed character into the buffer
  93.             sprintf(buffer,"%c",toupper(character[alpha]));
  94.  
  95.             if(SHADOW)
  96.                 {
  97.                 setcolor(tshadow);
  98.                 outtextxy(x+beta,y,"*");
  99.                 }
  100.  
  101.             setcolor(tcolor);
  102.             outtextxy(x+1+beta-offset,y+1-offset,"*");
  103.  
  104.             //Increment the character count and move to the next letter position
  105.             alpha++;
  106.             beta+=7;
  107.             }
  108.  
  109.         } //End of infinite for loop
  110.     if(character[alpha] == ESC)
  111.         {
  112.         ESC_pressed = 1;
  113.         }
  114.     else
  115.         {
  116.         //Clear any old values left in the text string
  117.         strcpy(text,"");
  118.  
  119.         //Write the characters into the text string
  120.         for(xx=0;xx<alpha;xx++)
  121.             {
  122.             sprintf(buffer,"%c",character[xx]);
  123.             strcat(text,buffer);
  124.  
  125.             //When end of string is encountered, return to calling program
  126.             if(character[xx] == '\0') break;
  127.             }
  128.         }
  129.     }
  130.